﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.AttributeSettingsPropertyDiscoverer
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using HarmonyLib.BUTR.Extensions;
using MCM.Abstractions;
using MCM.Abstractions.Attributes;
using MCM.Abstractions.Base;
using MCM.Abstractions.Properties;
using MCM.Abstractions.Wrapper;
using MCM.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

#nullable enable
namespace MCM.Implementation
{
  internal sealed class AttributeSettingsPropertyDiscoverer : 
    IAttributeSettingsPropertyDiscoverer,
    ISettingsPropertyDiscoverer
  {
    public IEnumerable<string> DiscoveryTypes { get; } = (IEnumerable<string>) new string[1]
    {
      "attributes"
    };

    public IEnumerable<ISettingsPropertyDefinition> GetProperties(BaseSettings settings)
    {
      foreach (ISettingsPropertyDefinition propertyDefinition in AttributeSettingsPropertyDiscoverer.GetPropertiesInternal(settings))
      {
        SettingsUtils.CheckIsValid(propertyDefinition, (object) settings);
        yield return propertyDefinition;
      }
    }

    private static IEnumerable<ISettingsPropertyDefinition> GetPropertiesInternal(
      BaseSettings settings)
    {
      Type type = settings.GetType();
      char subGroupDelimiter = (AccessTools2.Property(type, "SubGroupDelimiter")?.GetValue((object) settings) as char?).GetValueOrDefault('/');
      PropertyInfo[] propertyInfoArray = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
      for (int index = 0; index < propertyInfoArray.Length; ++index)
      {
        PropertyInfo property = propertyInfoArray[index];
        if (!(property.Name == "Id") && !(property.Name == "DisplayName") && !(property.Name == "FolderName") && !(property.Name == "FormatType") && !(property.Name == "SubFolder") && !(property.Name == "UIVersion"))
        {
          List<Attribute> attributes = property.GetCustomAttributes().ToList<Attribute>();
          object groupAttrObj = (object) attributes.SingleOrDefault<Attribute>((Func<Attribute, bool>) (a => a is IPropertyGroupDefinition));
          IPropertyGroupDefinition groupDefinition = groupAttrObj != null ? (IPropertyGroupDefinition) new PropertyGroupDefinitionWrapper(groupAttrObj) : SettingPropertyGroupAttribute.Default;
          List<IPropertyDefinitionBase> propertyDefinitions = SettingsUtils.GetPropertyDefinitionWrappers((IReadOnlyCollection<object>) attributes).ToList<IPropertyDefinitionBase>();
          if (propertyDefinitions.Count > 0)
            yield return (ISettingsPropertyDefinition) new SettingsPropertyDefinition((IEnumerable<IPropertyDefinitionBase>) propertyDefinitions, groupDefinition, (IRef) new PropertyRef(property, (object) settings), subGroupDelimiter);
          attributes = (List<Attribute>) null;
          groupAttrObj = (object) null;
          groupDefinition = (IPropertyGroupDefinition) null;
          propertyDefinitions = (List<IPropertyDefinitionBase>) null;
          property = (PropertyInfo) null;
        }
      }
      propertyInfoArray = (PropertyInfo[]) null;
    }
  }
}
